package com.du.linandroid.biz;

import android.app.Activity;

import com.du.linandroid.callback.CallBack;
import com.du.linandroid.util.Constant;
import com.du.linandroid.util.Util;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by DuZiLin on 2018/3/29.
 */

public class QQService implements IService {

    private Activity mActivity;
    private InnerUIListener loginLinstener;
    private CallBack outLoginCallBack;
    public QQService(Activity mActivity) {
        this.mActivity = mActivity;
        loginLinstener = new InnerUIListener();
    }

    @Override
    public void login(CallBack loginCallBack) {
        if(loginCallBack != null){
            outLoginCallBack = loginCallBack;
        }
        loginCallBack.start();
        if (!ApiHolder.mTencent.isSessionValid())
        {
            ApiHolder.mTencent.login(mActivity, "all", loginLinstener);
        }

    }

    class InnerUIListener implements IUiListener{

        @Override
        public void onComplete(Object o) {
            try {
                getUserInfo((JSONObject)o);
            } catch (JSONException e) {
                e.printStackTrace();
                outLoginCallBack.fail(Util.getFailJson(Constant.QQ_LOGIN_GET_ACCESS_TOKEN_FAILED_CODE, Constant.QQ_LOGIN_GET_ACCESS_TOKEN_FAILED_STR));
                outLoginCallBack.complete();
            }
        }

        @Override
        public void onError(UiError uiError) {
            outLoginCallBack.fail(Util.getFailJson(uiError.errorCode + "" , uiError.errorMessage));
            outLoginCallBack.complete();
        }

        @Override
        public void onCancel() {
            outLoginCallBack.fail(Util.getFailJson(Constant.QQ_LOGIN_FAILED_USER_CANCEL_CODE , Constant.QQ_LOGIN_FAILED_USER_CANCEL_STR));
            outLoginCallBack.complete();
        }
    }
    private void getUserInfo(JSONObject jsonObject) throws JSONException {
        String openID = jsonObject.getString("openid");
        String accessToken = jsonObject.getString("access_token");
        String expires = jsonObject.getString("expires_in");
        ApiHolder.mTencent.setOpenId(openID);
        ApiHolder.mTencent.setAccessToken(accessToken,expires);
        QQToken qqToken = ApiHolder.mTencent.getQQToken();
        UserInfo mUserInfo = new UserInfo(mActivity,qqToken);
        mUserInfo.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(Object o) {
                JSONObject json = (JSONObject) o;
                outLoginCallBack.success(json);
                outLoginCallBack.complete();
            }

            @Override
            public void onError(UiError uiError) {
                outLoginCallBack.fail(Util.getFailJson(uiError.errorCode+"" , uiError.errorMessage));
                outLoginCallBack.complete();
            }

            @Override
            public void onCancel() {
                outLoginCallBack.fail(Util.getFailJson(Constant.QQ_LOGIN_FAILED_USER_CANCEL_CODE , Constant.QQ_LOGIN_FAILED_USER_CANCEL_STR));
                outLoginCallBack.complete();
            }
        });
    }

    public InnerUIListener getLoginLinstener() {
        return loginLinstener;
    }
}
